<template>
	<view :style="containerStyle" class="container" @touchstart="touchStart" @touchend="touchEnd">
		<view class="plan-1 flex items-center justify-center" @click="goBack()">
			<up-icon name="arrow-left" class="tab-bar-icon" size="20" color="#fff" />
		</view>
		<view class="video-container" v-for="(item, index) in props.videoList" :key="item.id" :style="{ transform: `translateY(${(index - currentIndex) * 100}%)` }">
			<video
				:id="`video-${index}`"
				:src="item.video"
				class="video"
				:autoplay="!item.isPaused"
				:controls="true"
				:loop="true"
				:show-play-btn="true"
				:enable-progress-gesture="false"
				objectFit="contain"
				@ended="onVideoEnd(index)"
				@click="togglePlay(index)"
			/>

			<!-- 添加暂停图标 -->
			<!-- <view v-if="item.isPaused" class="pause-icon" @click="togglePlay(index)">
				<up-icon name="pause" color="#fff" size="30" />
			</view> -->

			<view class="video-desc line-3-clamp">{{ item.describe }}</view>

			<!-- 左侧作者信息 -->
			<view class="author-info flex items-center justify-center flex" v-if="item.user">
				<image :src="item.user?.photo" class="avatar" />
				<text class="nickname">{{ item.user?.nickname }}</text>
				<view class="date">· {{ item.create_date }}</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref, onMounted, onUnmounted, defineProps, nextTick, inject, computed } from 'vue';
	import { onReachBottom, onShow } from '@dcloudio/uni-app';
	import { port, hint, goBack } from '/service/port';

	const currentIndex = ref(0);

	const isLoading = ref(false);
	const isAnimating = ref(false);
	const pageSize = 5;
	const offsetTop = ref(10);
	const offsetHeight = ref(0);
	const props = defineProps({
		videoList: {
			type: Object,
			default: () => ({})
		}
	});

	const containerStyle = ref({
		height: '100vh'
	});

	// #ifdef MP-WEIXIN
	onMounted(() => {
		const menuInfo = inject('menuButtonInfo');
		// offsetHeight.value = menuInfo.top + menuInfo.bottom + 44;
		offsetTop.value = 25;
		containerStyle.value = {
			marginTop: menuInfo.top + 44 + 'px',
			marginBottom: menuInfo.bottom + 'px',
			height: `calc(100vh - ${menuInfo.top + 44}px)`
		};
	});
	console.log(containerStyle.value);
	// #endif

	// 新增点击切换播放状态方法
	const togglePlay = (index) => {
		if (index !== currentIndex.value) return;

		const videoItem = props.videoList[index];
		const videoContext = uni.createVideoContext(`video-${index}`);

		if (videoItem.isPaused) {
			videoContext.play();
		} else {
			videoContext.pause();
		}
		videoItem.isPaused = !videoItem.isPaused;
	};

	const handleVideoChange = (index) => {
		if (isAnimating.value || index < 0 || index >= props.videoList.length) return;
		isAnimating.value = true;

		// 停止当前视频并更新状态
		const currentVideo = props.videoList[currentIndex.value];
		if (currentVideo) {
			const videoContext = uni.createVideoContext(`video-${currentIndex.value}`);
			videoContext.pause();
			currentVideo.isPaused = true;
		}

		currentIndex.value = index;
		preloadNextVideo();

		// 开始新视频播放
		const newVideo = props.videoList[index];
		const newVideoContext = uni.createVideoContext(`video-${index}`);
		newVideoContext.play();
		newVideo.isPaused = false;

		setTimeout(() => {
			isAnimating.value = false;
		}, 300);
	};

	const preloadNextVideo = () => {
		const nextIndex = currentIndex.value + 1;
		// if (nextIndex >= props.videoList.length - 2) {
		// 	loadData(Math.floor(props.videoList.length / pageSize) + 1);
		// }
	};

	const handleLike = (item) => {
		item.likes += item.isLiked ? -1 : 1;
		item.isLiked = !item.isLiked;
	};

	const onVideoEnd = (index) => {
		if (index === currentIndex.value) {
			handleVideoChange(currentIndex.value + 1);
		}
	};

	let startY = 0;
	const touchStart = (e) => {
		startY = e.touches[0].pageY;
	};
	const touchEnd = (e) => {
		const deltaY = e.changedTouches[0].pageY - startY;
		if (Math.abs(deltaY) > 50 && !isAnimating.value) {
			handleVideoChange(currentIndex.value + (deltaY > 0 ? -1 : 1));
		}
	};

	onReachBottom(() => {
		handleVideoChange(currentIndex.value + 1);
	});

	onUnmounted(() => {
		const videoContext = uni.createVideoContext(`video-${currentIndex.value}`);
		videoContext.pause();
	});
</script>

<style lang="scss">
	::v-deep .uni-video-bar {
		position: absolute;
		bottom: 0;
		height: 50rpx;
	}
	::v-deep .uni-video-control-button {
		width: 13rpx;
		height: 15rpx;
		padding: 16.5px 12.5px 14.5px 8.5px;
	}
	::v-deep .uni-video-ball {
		width: 11px;
		height: 11px;
		padding-top: 16.1px;
	}
	::v-deep .uni-video-fullscreen {
		width: 13px;
		height: 13px;
		padding-top: 16px;
	}

	.plan-1 {
		background-color: rgb(0, 0, 0, 0.5);
		padding: 15rpx;
		border-radius: 50%;
		// margin: 30rpx;
		position: absolute;
		top: 20rpx;
		left: 20rpx;
		z-index: 999999;
	}

	.release-video {
		position: absolute;
		top: 60rpx;
		left: 40rpx;
		width: 68rpx;
		height: 68rpx;
		z-index: 9999;
	}

	.pause-icon {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		z-index: 10;
		background-color: rgb(0, 0, 0, 0.5);
		padding: 30rpx;
		border-radius: 50%;
	}
	.container {
		position: relative;
		background-color: #000;
		overflow: hidden;
		// height: 100vh;
	}

	.video-container {
		position: absolute;
		width: 100%;
		height: 100%;
		transition: transform 0.3s ease;
		will-change: transform;
	}

	.video {
		width: 100%;
		height: 100%;
	}

	.video-desc {
		position: absolute;
		bottom: 140rpx;
		left: 30rpx;
		margin: 24rpx 0;
		width: 60%;
		font-weight: 400;
		font-size: 26rpx;
		color: #ffffff;
	}

	.author-info {
		position: absolute;
		bottom: 80rpx;
		left: 30rpx;
		color: #fff;

		.avatar {
			width: 44rpx;
			height: 44rpx;
			border-radius: 50%;
		}

		.nickname {
			font-weight: 400;
			font-size: 20rpx;
			color: #ffffff;
			padding: 0 12rpx;
		}

		.date {
			font-weight: 400;
			font-size: 20rpx;
			color: #dcdbdb;
		}
	}

	.action-buttons {
		position: absolute;
		bottom: 80rpx;
		right: 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		z-index: 100;
		.button-item {
			margin-top: 40rpx;
			text-align: center;
			display: flex;
			flex-direction: column;
			.icon {
				width: 52rpx;
				height: 52rpx;
			}
			.count {
				font-weight: 400;
				font-size: 24rpx;
				color: #ffffff;
				margin-top: 6rpx;
			}
		}
	}
</style>
